


在電商平台上,當客戶加入多個商品到購物車時,需要計算總金額。這可以通過 for loop 來累加購物車中所有商品的價格。
# 假設客戶購物車中的商品
cart = {'Product A': 2, 'Product B': 1, 'Product C': 3}  # 產品和數量
prices = {'Product A': 100, 'Product B': 150, 'Product C': 200}
total_amount = 0
for product, quantity in cart.items():
    total_amount += prices[product] * quantity
    print(f"{product}: {quantity} x ${prices[product]} = ${prices[product] * quantity}")
print(f"Total cart amount: ${total_amount}")
連鎖店的調貨情境,可以假設一個總部需要從不同的分店調貨。當某個分店的庫存不足時,總部會轉向其他分店直到調足訂單需求。
# 各連鎖店的庫存數據
stores = {
    'Store A': 30,
    'Store B': 50,
    'Store C': 20,
    'Store D': 40
}
# 訂單需求的數量
order_quantity = 100
# 初始化變數
total_sourced = 0  # 已調到的總數量
current_store_index = 0  # 目前正在查詢的店鋪索引
store_list = list(stores.keys())  # 將店鋪名單轉換成列表,便於索引遍歷
while total_sourced < order_quantity and current_store_index < len(store_list):
    current_store = store_list[current_store_index]
    available_stock = stores[current_store]
    if available_stock > 0:
        # 計算從該店鋪能調到的數量
        to_source = min(order_quantity - total_sourced, available_stock)
        total_sourced += to_source
        stores[current_store] -= to_source
        print(f"Fetched {to_source} units from {current_store}. Total sourced: {total_sourced} units.")
    else:
        print(f"{current_store} has no stock left.")
    # 移動到下一個店鋪
    current_store_index += 1
# 調貨結果
if total_sourced >= order_quantity:
    print(f"Order fulfilled! Total sourced: {total_sourced} units.")
else:
    print(f"Unable to fully source the order. Total sourced: {total_sourced} units, {order_quantity - total_sourced} units still needed.")
如何在達到訂單需求時使用 break 立即結束調貨流程。
# 各分公司的庫存數據
branches = {
    'Branch A': 30,
    'Branch B': 50,
    'Branch C': 20,
    'Branch D': 40
}
# 訂單需求的數量
order_quantity = 70
# 初始化變數
total_sourced = 0  # 已調到的總數量
current_branch_index = 0  # 目前正在查詢的分公司索引
branch_list = list(branches.keys())  # 將分公司名單轉換成列表,便於索引遍歷
while current_branch_index < len(branch_list):
    current_branch = branch_list[current_branch_index]
    available_stock = branches[current_branch]
    if available_stock > 0:
        to_source = min(order_quantity - total_sourced, available_stock)
        total_sourced += to_source
        branches[current_branch] -= to_source
        print(f"Fetched {to_source} units from {current_branch}. Total sourced: {total_sourced} units.")
        # 如果已經達到訂單需求,結束調貨
        if total_sourced >= order_quantity:
            print("Order fulfilled!")
            break  # 立即結束迴圈
    else:
        print(f"{current_branch} has no stock left.")
    current_branch_index += 1
if total_sourced < order_quantity:
    print(f"Unable to fully source the order. Still need {order_quantity - total_sourced} units.")
使用 continue 當分公司庫存為 0 時跳過該公司,並繼續查找下一個分公司。
# 各分公司的庫存數據
branches = {
    'Branch A': 0,   # 無庫存
    'Branch B': 50,
    'Branch C': 0,   # 無庫存
    'Branch D': 40
}
# 訂單需求的數量
order_quantity = 80
# 初始化變數
total_sourced = 0  # 已調到的總數量
current_branch_index = 0  # 目前正在查詢的分公司索引
branch_list = list(branches.keys())  # 將分公司名單轉換成列表,便於索引遍歷
while current_branch_index < len(branch_list):
    current_branch = branch_list[current_branch_index]
    available_stock = branches[current_branch]
    # 如果該分公司無庫存,跳過該分公司
    if available_stock == 0:
        print(f"{current_branch} has no stock, moving to next branch.")
        current_branch_index += 1
        continue  # 跳過剩餘的處理,繼續下一個迴圈
    to_source = min(order_quantity - total_sourced, available_stock)
    total_sourced += to_source
    branches[current_branch] -= to_source
    print(f"Fetched {to_source} units from {current_branch}. Total sourced: {total_sourced} units.")
    if total_sourced >= order_quantity:
        print("Order fulfilled!")
        break
    current_branch_index += 1
if total_sourced < order_quantity:
    print(f"Unable to fully source the order. Still need {order_quantity - total_sourced} units.")